/*
 * AddFeat.cpp
 *
 *  Created on: Jan 11, 2009
 *      Author: sez
 */

#include "AddFeat.h"
#include "ui_addfeat.h"

#include <DndData/Feat.h>

AddFeat::AddFeat(QWidget *parent)
    : AddUpdateDialog(XmlPath(XmlPath::FEAT_BASE), parent)
    , mAddFeat(new Ui::AddFeatDialog())
    , mData(0)
{
    mAddFeat->setupUi(this);
}

AddFeat::~AddFeat()
{
    delete mAddFeat;
    delete mData;
}

TitledData *AddFeat::data()
{
    if (!mData)
        mData = new Feat(xmlPath().typeName());
    return mData;
}

/*!
  \brief Return the instance of the AddFeat dialog.
  This method is first called during the construction of the QMainWindow,
  and at this time the instance is initialized with the main window as its
  \a parent.  With this invariant, the method is thread-safe, otherwise it
  is not.  On subsequent calls the already initialized instance is returned
  and any argument is ignored.
 */
AddFeat *AddFeat::dialog(QWidget *parent)
{
    static AddFeat *dlg = 0;
    if (dlg == 0)
        dlg = new AddFeat(parent);
    return dlg;
}

void AddFeat::loadData()
{
    QString classOrRace;
    if (mData->type == Feat::CLASS_FEAT)
    {
        if (mData->extClassName.isEmpty())
            classOrRace = DndData::CLASS_NAMES[mData->className];
        else
            classOrRace = mData->extClassName;
    }
    else if (mData->type == Feat::RACIAL_FEAT)
    {
        if (mData->extRace.isEmpty())
            classOrRace = DndData::RACE_NAMES[mData->raceName];
        else
            classOrRace = mData->extRace;
    }
    mAddFeat->summaryLabel->setText(QString("%1 %2 Feat").arg(classOrRace)
                                    .arg(Feat::FEAT_TYPE_NAMES[mData->type]));
    mAddFeat->tierComboBox->setCurrentIndex(mData->tier);
    mAddFeat->prerequisitesTextEdit->document()->setPlainText(mData->prerequisites);
    mAddFeat->benefitsTextEdit->document()->setPlainText(mData->benefits);
    mAddFeat->specialTextEdit->document()->setPlainText(mData->special);
    mAddFeat->typeComboBox->setCurrentIndex(mData->type);
    mAddFeat->classFeatComboBox->setCurrentIndex(mData->className);
    mAddFeat->racialFeatComboBox->setCurrentIndex(mData->raceName);
}

void AddFeat::saveData()
{
    mData->tier = static_cast<DndData::Tier>(mAddFeat->tierComboBox->currentIndex());
    mData->prerequisites = mAddFeat->prerequisitesTextEdit->document()->toPlainText();
    mData->benefits = mAddFeat->benefitsTextEdit->document()->toPlainText();
    mData->special = mAddFeat->specialTextEdit->document()->toPlainText();
    mData->type = static_cast<Feat::FeatType>(mAddFeat->typeComboBox->currentIndex());
    mData->raceName = static_cast<DndData::RaceName>(mAddFeat->racialFeatComboBox->currentIndex());
    mData->className = static_cast<DndData::ClassName>(mAddFeat->classFeatComboBox->currentIndex());
}

void AddFeat::setupData()
{
    for (int i = 0; i < DndData::NUM_TIERS; ++i)
        mAddFeat->tierComboBox->addItem(DndData::TIER_NAMES[i]);
    for (int i = 0; i < Feat::NUM_FEAT_TYPES; ++i)
        mAddFeat->typeComboBox->addItem(Feat::FEAT_TYPE_NAMES[i]);
    for (int i = 0; i < DndData::NUM_CLASSES; ++i)
        mAddFeat->classFeatComboBox->addItem(DndData::CLASS_NAMES[i]);
    for (int i = 0; i < DndData::NUM_RACES; ++i)
        mAddFeat->racialFeatComboBox->addItem(DndData::RACE_NAMES[i]);
}

void AddFeat::clearData()
{
    mAddFeat->tierComboBox->setCurrentIndex(0);
    mAddFeat->prerequisitesTextEdit->document()->setPlainText("");
    mAddFeat->benefitsTextEdit->document()->setPlainText("");
    mAddFeat->specialTextEdit->document()->setPlainText("");
    mAddFeat->typeComboBox->setCurrentIndex(Feat::STANDARD_FEAT);
    mAddFeat->classFeatComboBox->setCurrentIndex(0);
    mAddFeat->racialFeatComboBox->setCurrentIndex(0);
}

void AddFeat::on_typeComboBox_currentIndexChanged(int index)
{
    mAddFeat->classFeatComboBox->setEnabled(index == Feat::CLASS_FEAT);
    mAddFeat->racialFeatComboBox->setEnabled(index == Feat::RACIAL_FEAT);
}
